package com.boyue.controller;

import com.boyue.domain.RespBean;
import com.boyue.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private DiscoveryClient client;

    @GetMapping("/info")
    public RespBean getInfo(){
        User user=new User("boyue","password","24");
        return RespBean.ok("用户信息：",user);
    }

    @Value("${server.port}")
    private String port;
    @Value("${spring.application.name}")
    private String applicationName;
    @GetMapping("/eurekaClientInfo")
    public RespBean eurekaClientInfo(){
       String info="被调用服务的名称："+applicationName+",服务的端口："+port;
        return RespBean.ok("服务信息如下：",info);
    }

    @GetMapping("/getClientInfo")
    public RespBean getClientInfo(){
        List<String> services = client.getServices();
        System.out.println("discovery---》services:" + services);
        List<Object> res=new ArrayList<>();
        List<ServiceInstance> instances = client.getInstances("EUREKA-CLIENT");
        for(ServiceInstance item: instances){
            Map<String,Object> info=new HashMap<>();
            info.put("host",item.getHost());
            info.put("port",item.getPort());
            info.put("uri",item.getUri());
            info.put("instanceid",item.getInstanceId());
            info.put("serviceid",item.getServiceId());
            System.out.println("host: "+item.getHost());
            System.out.println("posrt: "+item.getPort());
            System.out.println("uri: "+item.getUri());
            System.out.println("instanceid: "+item.getInstanceId());
            System.out.println("serviceid: "+item.getServiceId());
            res.add(info);
        }

        return RespBean.ok("discoveryInfo:"+res);


    }

}
